﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Arcadia.Bookasa.Common;
using Arcadia.Bookasa.Common.Exception;
using Arcadia.Bookasa.Common.Watch;
using Arcadia.Bookasa.Common.Scan;
using Arcadia.Bookasa.Common.Service;
using Arcadia.Bookasa.Common.Storage;

namespace Arcadia.Bookasa.Common.Service
{
    public abstract class BaseServiceExtension
    {
        protected IServiceHost _serviceHost;

        
        /// <summary>
        /// Initialize the Source Monitor as a Service Extension.
        /// </summary>
        /// <param name="config">configuration </param>
        /// <param name="serviceHost">service host which init the extension</param>
        public virtual void Activate(IServiceHost serviceHost)
        {
            if (serviceHost == null)
            {
                throw new InitializeParameterEmtyException("parameter can't be null for initialize");
            }

            _serviceHost = serviceHost;
        }

        /// <summary>
        /// Check whether the give Type is supported.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public bool IsSupport<T>()
        {
            return this.GetType().IsAssignableFrom(typeof(T));
        }
        
        /// <summary>
        /// Return the required interface.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns>null or the interface</returns>
        public T GetInterface<T>()
        {
            if (typeof(T).IsInterface && this.IsSupport<T>())
                return (T)(object)this;
            else
                return default(T);
        }

        public virtual void Deactivate()
        {
        }
    }
}
